package com.yingjia.yingjiauniversity.retrofit.convert;

import com.yingjia.yingjiauniversity.json.manager.JsonManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.lang.reflect.Type;

import okhttp3.ResponseBody;
import retrofit2.Converter;

/**
 * Created by 张智轶 on 2016/10/12.
 */
public class ObjectResponseBodyConverter implements Converter<ResponseBody, Object> {
    protected Logger logger = LoggerFactory.getLogger(((Object) this).getClass());
    Type mType;

    public ObjectResponseBodyConverter(Type type) {
        mType = type;
    }

    @Override
    public Object convert(ResponseBody value) throws IOException {
        String jsonString = value.string();
        try {
            return JsonManager.getInstance().getComponent().toObject(jsonString, mType, null);
        } catch (Exception e) {
            logger.error("convert: value=[{}]", value, e);
        }
        return null;
    }
}
